package struts.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.Choose;
import models.Student;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import struts.forms.StudentForm;
import biz.studentDiy.StudentDiyBiz;
import biz.studentDiy.StudentDiyBizImpl;

public class StuDiyAction extends MappingDispatchAction {
	private StudentDiyBiz stuDiyBiz = new StudentDiyBizImpl();

	public ActionForward loadStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		StudentForm stuForm = (StudentForm) form;
		Student stu = (Student) session.getAttribute("student");
		stuForm.setStudent(stu);
		return mapping.findForward("modify");
	}

	public ActionForward modifyStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm stuForm = (StudentForm) form;
		Student stu = stuForm.getStudent();
		stuDiyBiz.modfiyStudent(stu);
		return mapping.findForward("modifySuccess");
	}

	public ActionForward getChooses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		Student stu=(Student) session.getAttribute("student");
		Long stuId=stu.getId();
		List<Choose> chooses=(List<Choose>) stuDiyBiz.getAllChooseByStuId(stuId);
		request.setAttribute("chooses", chooses);
		
		return mapping.findForward("chooses");
	}
}
